package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.mapper.mysql.*;
import com.jy.adscm.wechat.service.WechatMaterialService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.wechat.service.WechatUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-29
 */
@Service
public class WechatMaterialServiceImpl extends ServiceImpl<WechatMaterialMapper, WechatMaterial> implements WechatMaterialService {
    @Autowired
    CgMaterialMapper  cgMaterialMapper;
    @Autowired
    ShCustomerInfoMapper shCustomerInfoMapper;
    @Autowired
    WechatUserRoleService wechatUserRoleService;
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    WechatMaterialMapper wechatMaterialMapper;
    @Autowired
    WechatPartsMapper wechatPartsMapper;

    @Override
    public Page<WechatMaterial> selectWechatMaterial(Page<WechatMaterial> page, QueryWechatMaterial queryWechatMaterial) throws AdscmSystemException {
        Boolean jzUser = wechatUserRoleService.isJzUser(queryWechatMaterial.getOpenid());
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(queryWechatMaterial.getOpenid());
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            return page;
        }
        Wrapper<WechatMaterial> wrapper =new EntityWrapper<>();
        if (jzUser){
            wrapper.eq("pid",wechatUser.getPlasmaStationId());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryWechatMaterial.getIsSign(),null)){
            wrapper.eq("is_sign",queryWechatMaterial.getIsSign());
        }

        page=super.selectPage(page,wrapper);
        List<WechatMaterial> wechatMaterials = page.getRecords();
        for (WechatMaterial wechatMaterial : wechatMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterial.getName(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(wechatMaterial.getName());
                wechatMaterial.setCgMaterial(cgMaterial);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterial.getPid(),null)){
                ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatMaterial.getPid());
                wechatMaterial.setShCustomerInfo(shCustomerInfo);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterial.getCreator(),null)){
                WechatUser wechatUser1 = wechatUserMapper.selectById(wechatMaterial.getCreator());
                wechatMaterial.setCreatorName(wechatUser1.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterial.getSigner(),null)){
                WechatUser wechatUser1 = wechatUserMapper.selectById(wechatMaterial.getSigner());
                wechatMaterial.setSignerName(wechatUser1.getName());
            }
        }
        page.setTotal(selectCount(wrapper));

        return page;
    }
     @Transactional(rollbackFor = Exception.class)
    @Override
    public WechatMaterial updateWechatMaterialVaule(Long pid, Float nums)throws AdscmSystemException {
        synchronized (this){
            WechatMaterial wechatMaterial = super.selectById(pid);
            if (nums>0){
                if (nums>wechatMaterial.getRemain()){
                    throw new  AdscmSystemException("使用数量大于剩余数量");
                }
            }
            if (wechatMaterial.getRemain()-nums==0){
                wechatMaterial.setIsOut(1);
            }else {
                wechatMaterial.setIsOut(0);
            }
            wechatMaterial.setRemain(wechatMaterial.getRemain()-nums);
            super.updateById(wechatMaterial);
            return wechatMaterial;
        }

    }

    @Override
    public WechatMaterial selectWechatMaterialById(Long id) {
        WechatMaterial wechatMaterial = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterial.getName(),null)){
            CgMaterial cgMaterial = cgMaterialMapper.selectById(wechatMaterial.getName());
            wechatMaterial.setCgMaterial(cgMaterial);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterial.getPid(),null)){
            ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatMaterial.getPid());
            wechatMaterial.setShCustomerInfo(shCustomerInfo);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterial.getCreator(),null)){
            WechatUser wechatUser1 = wechatUserMapper.selectById(wechatMaterial.getCreator());
            wechatMaterial.setCreatorName(wechatUser1.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterial.getSigner(),null)){
            WechatUser wechatUser1 = wechatUserMapper.selectById(wechatMaterial.getSigner());
            wechatMaterial.setSignerName(wechatUser1.getName());
        }
        return wechatMaterial;
    }

    @Override
    public List<WeChatResource> selectInStock(String keywords,String openid,Integer  pid) throws AdscmSystemException {
        if (!AdscmBaseUtils.getInstance().isNotEmpty(pid,null)){
            WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatUser.getPlasmaStationId(),null)){
                pid=wechatUser.getPlasmaStationId();
            }

        }
        List<WeChatResource> weChatResources =new ArrayList<>();

        List<WechatMaterial> wechatMaterials= wechatMaterialMapper.selectInStock(keywords,pid.toString());
        for (WechatMaterial wechatMaterial : wechatMaterials) {
            WeChatResource weChatResource=new WeChatResource();
            weChatResource.setType(1);
            weChatResource.setId(wechatMaterial.getId());
            weChatResource.setPid(wechatMaterial.getPid());
            ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatMaterial.getPid());
            weChatResource.setShCustomerInfo(shCustomerInfo);
            weChatResource.setName(wechatMaterial.getCgMaterial().getName());
            weChatResource.setTypeNums(wechatMaterial.getCgMaterial().getType());
            weChatResource.setBatch(wechatMaterial.getBatch());
            weChatResource.setCode(wechatMaterial.getCode());
            weChatResource.setNums(wechatMaterial.getNums());
            weChatResource.setAutNums(wechatMaterial.getAutNums());
            weChatResource.setRemain(wechatMaterial.getRemain());
            weChatResources.add(weChatResource);
        }
         List<WechatParts> wechatParts=wechatPartsMapper.selectInStock(keywords,pid.toString());
        for (WechatParts wechatPart : wechatParts) {
            WeChatResource weChatResource=new WeChatResource();
            weChatResource.setType(2);
            weChatResource.setId(wechatPart.getId());
            weChatResource.setPid(wechatPart.getPid());
            ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatPart.getPid());
            weChatResource.setShCustomerInfo(shCustomerInfo);
            weChatResource.setName(wechatPart.getJqbzSettings().getName());
            weChatResource.setTypeNums(wechatPart.getJqbzSettings().getPartType());
            weChatResource.setBatch(wechatPart.getBatch());
            weChatResource.setCode(wechatPart.getCode());
            weChatResource.setNums(wechatPart.getNums());
            weChatResource.setAutNums(wechatPart.getAutNums());
            weChatResource.setRemain(wechatPart.getRemain());
            weChatResources.add(weChatResource);
        }
        return weChatResources;
    }

    @Override
    public Boolean updateWechatMaterial(WechatMaterial wechatMaterial, String openid) throws AdscmSystemException{
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
        WechatMaterial oldWechatMaterial= super.selectById(wechatMaterial.getId());
        if (wechatMaterial.getIsSign()==1){
            if (oldWechatMaterial.getIsSign()==0){
                wechatMaterial.setSigner(wechatUser.getId().toString());
                if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterial.getAutNums(),null)){
                    throw new AdscmSystemException("请填写实际到货数量");
                }
                wechatMaterial.setRemain(wechatMaterial.getAutNums());
                wechatMaterial.setSignTime(new Date());
            }
        }
        boolean b = super.updateById(wechatMaterial);
        return b;
    }
}
